package com.hengfoxwolf.jetpackdemo.base
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

abstract class BaseFragment<VB:ViewBinding>(val block: (LayoutInflater, ViewGroup?, Boolean) -> VB) : Fragment() {

    private lateinit var _binding: VB

    protected val mBinding get() = _binding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = block(layoutInflater, container, false)
        return _binding.root
    }

    operator fun TextView.provideDelegate(value:Any?, property: KProperty<*>) = object :
        ReadWriteProperty<Any?, String?> {
        override fun getValue(thisRef: Any?, property: KProperty<*>): String? {
            return text as String?
        }

        override fun setValue(thisRef: Any?, property: KProperty<*>, value: String?) {
            text = value
        }
    }
}